package com.central.apps.handler;

import cn.hutool.core.util.IdUtil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.exception.service.ServiceException;
import com.central.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.expression.MapAccessor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class MaterialTypeManageHandler extends LvBaseHandler {


    @Autowired
    private HandlerService handlerService;

    @Override
    public String validateDelete(String app, String objName, Long id) throws Exception {
        if("itemtypeApp".equals(app)){
            List<String> containList = new ArrayList<>();
            if("gq_itemtype".equals(objName)){
                List<Map<String, Object>> bySql = handlerService.findBySql("select maxnum from gq_itemtype where id = " + id);
                if(CollectionUtils.isNotEmpty(bySql)){
                    Map<String, Object> map = bySql.get(0);
                    String maxnum = MapUtils.getString(map, "maxnum");
                    List<Map<String, Object>> bySql1 = handlerService.findBySql("SELECT SUBSTR(itemnum FROM 3 FOR 1) as itemnum from gq_item");
                    if(CollectionUtils.isNotEmpty(bySql1)){
                        for (Map<String, Object> stringObjectMap : bySql1) {
                            containList.add(MapUtils.getString(stringObjectMap,"itemnum"));
                        }
                        if(CollectionUtils.isNotEmpty(containList)){
                            if(containList.contains(maxnum)){
                                return "大类编码为"+maxnum+"已经有物料生成，不允许删除";
                            }
                        }
                    }
                }
            }
            if("gq_medium".equals(objName)){
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_medium where id = " + id);
                if(CollectionUtils.isNotEmpty(bySql)){
                    Map<String, Object> map = bySql.get(0);
                    String maxnum = MapUtils.getString(map, "maxnum");
                    String mediumnum = MapUtils.getString(map, "mediumnum");
                    List<Map<String, Object>> bySql1 = handlerService.findBySql("SELECT SUBSTR(itemnum FROM 4 FOR 2) as itemnum  from gq_item where SUBSTR(itemnum FROM 3 FOR 1) = '" + maxnum + "'");//大类编码
                    if(CollectionUtils.isNotEmpty(bySql1)){
                        for (Map<String, Object> stringObjectMap : bySql1) {
                            containList.add(MapUtils.getString(stringObjectMap,"itemnum"));
                        }
                    }
                    if(CollectionUtils.isNotEmpty(containList)){
                        if(containList.contains(mediumnum)){
                            return "大类编码为"+maxnum+"中类编码为"+ mediumnum +"已经有物料生成，不允许删除";
                        }
                    }
                }
            }
            if("gq_small".equals(objName)){
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_small where id = " + id);
                if(CollectionUtils.isNotEmpty(bySql)){
                    Map<String, Object> map = bySql.get(0);
                    String maxnum = MapUtils.getString(map, "maxnum");
                    String mediumnum = MapUtils.getString(map, "mediumnum");
                    String smallnum = MapUtils.getString(map, "smallnum");
                    List<Map<String, Object>> bySql1 = handlerService.findBySql("SELECT SUBSTR(itemnum FROM 6 FOR 2) as itemnum from gq_item where SUBSTR(itemnum FROM 3 FOR 1) = '" + maxnum + "' and SUBSTR(itemnum FROM 4 FOR 2) = '" + mediumnum + "'");
                    if(CollectionUtils.isNotEmpty(bySql1)){
                        for (Map<String, Object> stringObjectMap : bySql1) {
                            containList.add(MapUtils.getString(stringObjectMap,"itemnum"));
                        }
                    }
                    if(CollectionUtils.isNotEmpty(containList)){
                        if(containList.contains(smallnum)){
                            return "大类编码为"+maxnum+"中类编码为"+ mediumnum +"小类编码为"+ smallnum +"已经有物料生成，不允许删除";
                        }
                    }
                }
            }
        }
        return super.validateDelete(app, objName, id);
    }

    @Override
    public void beforDelete(String app, String objName, Long id, boolean isLogic) {
        //删除物料类型，将其下面的子表数据删除
        if("itemtypeApp".equals(app)){
            if("gq_itemtype".equals(objName)){
                List<Map<String, Object>> bySql = handlerService.findBySql("select maxnum from gq_itemtype where id = " + id);
                if(CollectionUtils.isNotEmpty(bySql)){
                    Map<String, Object> map = bySql.get(0);
                    String maxnum = MapUtils.getString(map, "maxnum");
                    handlerService.findBySql("delete from gq_medium where maxnum = '" + maxnum + "'");
                    handlerService.findBySql("delete from gq_small where maxnum = '" + maxnum + "'");
                }
            }
            if("gq_medium".equals(objName)){
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_medium where id = " + id);
                if(CollectionUtils.isNotEmpty(bySql)){
                    Map<String, Object> map = bySql.get(0);
                    String maxnum = MapUtils.getString(map, "maxnum");
                    String mediumnum = MapUtils.getString(map, "mediumnum");
                    handlerService.findBySql("delete from gq_small where maxnum = '" + maxnum + "' and mediumnum = '" + mediumnum + "'");
                }
            }
        }
        super.beforDelete(app, objName, id, isLogic);
    }

    //物料大类
    @Override
    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("itemtypeApp".equals(app)) {
//            //新增保存后中类，小类都都要有才可以进行保存
//            List<Map<String, Object>> tableUKey78320=new ArrayList<>();
            String maxnum = MapUtils.getString(data, "maxnum");
            String maxdesc = MapUtils.getString(data, "maxdesc");

            if(isUpdate==false){
                if ("gq_itemtype".equals(objName)) {
                    String maxnumSql = "select * from gq_itemtype where maxnum ='"+maxnum+"'";
                    List<Map<String, Object>> bySql = handlerService.findBySql(maxnumSql);
                    if (bySql != null && bySql.size() > 0) {
                        throw new ServiceException("物料大类里面已存在编码为" + maxnum + "的物料，请重新输入!");
                    }
                    String maxdescSql = "select * from gq_itemtype where maxdesc ='"+maxdesc+"'";
                    List<Map<String, Object>> bySql1 = handlerService.findBySql(maxdescSql);
                    if (bySql1 != null && bySql1.size() > 0) {
                        throw new ServiceException("物料大类里面已存在名称为" + maxdesc + "的物料，请重新输入!");
                    }
                    //物料类型串存数据
                    data.put("materialtypestring",maxnum);
                }
                if("gq_medium".equals(objName)){//物料中类
                    String mediumnum = MapUtils.getString(data, "mediumnum");
                    String mediumdesc= MapUtils.getString(data, "mediumdesc");
                    if(mediumdesc.length()>50){
                        throw new ServiceException("中类描述数据长度超过最大长度50!");
                    }
                    int length = mediumnum.length();
                    if(length>2){
                        throw new ServiceException("中类编码数据长度超过最大长度2!");
                    }
                    String maxnumSql="select * from gq_medium where maxnum ='"+maxnum+"' and mediumnum = "+ mediumnum;
                    List<Map<String, Object>> bySql = handlerService.findBySql(maxnumSql);
                    if(bySql!=null && bySql.size()>0){
                        throw new ServiceException("该物料大类里面已存在中类编码为"+mediumnum+"的物料，请重新输入!");
                    }
                    String mediumdescSql="select * from gq_medium where mediumdesc ='"+mediumdesc + "'";
                    List<Map<String, Object>> bySql1 = handlerService.findBySql(mediumdescSql);
                    if(bySql1!=null && bySql1.size()>0){
                        throw new ServiceException("物料中类里面已存在中类名称为"+mediumdesc+"的物料，请重新输入!");
                    }
                    String uid = IdUtil.simpleUUID();
                    data.put("uuidprice",uid);
                    //物料类型串存数据
                    data.put("materialtypestring",maxnum+mediumnum);
                }
                if("gq_small".equals(objName)){
                    String mediumnum = MapUtils.getString(data, "mediumnum");
                    String smallnum = MapUtils.getString(data, "smallnum");
                    String smalldesc= MapUtils.getString(data, "smalldesc");
                    if(StringUtils.isBlank(mediumnum)){
                        throw new ServiceException("请先新建物料中类或请先选择物料中类后再进行新建小类编码");
                    }
                    if(smalldesc.length()>50){
                        throw new ServiceException("小类描述数据长度超过最大长度50!");
                    }
                    int length = smallnum.length();
                    if(length>2){
                        throw new ServiceException("小类编码数据长度超过最大长度2!");
                    }
                    String smallnumSql="select * from gq_small where maxnum='"+maxnum+"' and mediumnum = "+ mediumnum + " and smallnum = "+ smallnum;
                    List<Map<String, Object>> bySql = handlerService.findBySql(smallnumSql);
                    if(bySql!=null && bySql.size()>0){
                        throw new ServiceException("该物料大类里面已存在小类编码为"+smallnum+"的物料，请重新输入!");
                    }
                    String smalldescSql="select * from gq_small where smalldesc='"+smalldesc + "'";
                    List<Map<String, Object>> bySql1 = handlerService.findBySql(smalldescSql);
                    if(bySql1!=null && bySql1.size()>0){
                        throw new ServiceException("物料小类里面已存在小类名称为"+smalldesc+"的物料，请重新输入!");
                    }
                    //物料类型串存数据
                    data.put("materialtypestring",maxnum+mediumnum+smallnum);
                }
            }
        }
        super.beforSave(app, objName, data, isUpdate);
    }

//    @Override
//    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
//        if ("itemtypeApp".equals(app)) {
//            String maxnum = MapUtils.getString(data, "maxnum");
//            List<Map<String, Object>> bySql1 = handlerService.findBySql("select count(0) as count from gq_medium where maxnum = '" + maxnum + "'");
//            if(MapUtils.getInteger(bySql1.get(0),"count")<=0){
//                throw new ServiceException("请输入物料中类编码再进行保存");
//            }
//            List<Map<String, Object>> bySql2 = handlerService.findBySql("select count(0) as count from gq_small where maxnum = '" + maxnum + "'");
//            if(MapUtils.getInteger(bySql2.get(0),"count")<=0){
//                throw new ServiceException("请输入物料小类编码再进行保存");
//            }
//        }
//
//        super.afterSave(app, objName, data, isUpdate);
//    }


    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if("itemtypeApp".equals(app)){
            if("verificationType".equals(type)){
                String maxnum = MapUtils.getString(params, "maxnum");
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select count(0) as count from gq_medium where maxnum = '" + maxnum + "'");
                if(MapUtils.getInteger(bySql1.get(0),"count")<=0){
                    handlerService.findBySql("delete from gq_itemtype where maxnum = '"+ maxnum+ "'");
                    return Result.failed("请输入物料中类编码再进行保存");
                }
                List<Map<String, Object>> bySql2 = handlerService.findBySql("select count(0) as count from gq_small where maxnum = '" + maxnum + "'");
                if(MapUtils.getInteger(bySql2.get(0),"count")<=0){
                    handlerService.findBySql("delete from gq_itemtype where maxnum = '"+ maxnum+ "'");
                    handlerService.findBySql("delete from gq_medium where maxnum = '"+ maxnum+ "'");
                    return Result.failed("请输入物料小类编码再进行保存");
                }
            }
        }
        return super.operate(app, type, params);
    }

    //去掉校验
//    @Override
//    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
//        if("itemtypeApp".equals(app) && "gq_itemtype".equals(objName)){
//            List<Map<String, Object>> tableUKey78320 = (List<Map<String, Object>>)data.get("TableUKey78320");
//            if(CollectionUtils.isEmpty(tableUKey78320)){
//                return "请先填写中类编码再进行保存";
//            }
//            for (Map<String, Object> map : tableUKey78320) {
//                List<Map<String, Object>> tableUKey78330list = (List<Map<String, Object>>)map.get("TableUKey78330");
//                if(CollectionUtils.isEmpty(tableUKey78330list)){
//                    return "请先填写小类编码再进行保存";
//                }
//            }
//        }
//        return super.validateSave(app, objName, data);
//    }


    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        String id = MapUtils.getString(data, "id");
        if("itemtypeApp".equals(app)&&"gq_itemtype".equals(objName)){
            if(isUpdate==false){
                //新增修改
                String uid = IdUtil.simpleUUID();
                handlerService.findBySql("update gq_itemtype set uuidprice = '" + uid + "' where id = '" + id+ "'");
            }
            handlerService.findBySql("update gq_itemtype set disablebutton = '1' where id = '" + id+ "'");
        }else if("itemtypeApp".equals(app)&&"gq_medium".equals(objName)){
            if(isUpdate==false){
                //新增修改
                String uid = IdUtil.simpleUUID();
                handlerService.findBySql("update gq_medium set uuidprice = '" + uid + "' where id = '" + id+ "'");
            } else {
                String maxNum = MapUtils.getString(data, "maxnum");
                String mediumNum = MapUtils.getString(data, "mediumnum");
                String mediumDesc = MapUtils.getString(data, "mediumdesc");

                handlerService.updateBySql("update gq_small set mediumdesc = '" + mediumDesc + "' where maxnum = '" + maxNum + "' and mediumnum = '" + mediumNum + "'");
            }
        }else if("itemtypeApp".equals(app)&&"gq_small".equals(objName)){
            if(isUpdate==false){
                //新增修改
                String uid = IdUtil.simpleUUID();
                handlerService.findBySql("update gq_small set uuidprice = '" + uid + "' where id = '" + id+ "'");
            }
        }
        super.afterSave(app, objName, data, isUpdate);
    }
}
